<?php

class Admin_UserController extends Zend_Controller_Action
{

	public function init(){
		      
		  /*$templateConfig = APPLICATION_PATH . '/templates/admin/template.ini';        
       
	      $config = new Zend_Config_Ini($templateConfig, 'template');      
	      
	      $urlTemplate = $this->_request->getBaseUrl().$config->url;
	      $dirCss = $urlTemplate.$config->dir->css;
	      $dirJs = $urlTemplate.$config->dir->js;
	      $dirImg = $urlTemplate.$config->dir->img;      
	      $this->view->dirImg = $dirImg;
	      
	      foreach($config->file->js as $key => $item){         
	         $this->view->headScript()->appendFile($dirJs.$item,'text/javascript');   
	      }
	      
	      $layoutPath = $config->folder;
	      $option = array ('layout' => 'index',
	                   'layoutPath' => $layoutPath );
	      Zend_Layout::startMvc ( $option );*/
		  $this->view->headScript()->appendFile(APPLICATION_PATH.'/templates/admin/default/js/jquery-1.7.min.js','text/javascript');
		  $this->view->headScript()->appendFile(APPLICATION_PATH.'/templates/admin/default/js/jquery.validate.js','text/javascript');
	      $layoutPath = APPLICATION_PATH  . '/templates/admin/default';
	      $option = array ('layout' => 'index', 
	                   'layoutPath' => $layoutPath );
	      Zend_Layout::startMvc ( $option );

	}

    public function indexAction()
    {
        // action body
    }

    public function managerAction(){
    	include_once dirname(__FILE__).'./../models/UserModel.php';
        $userModel = new UserModel();
        $users = $userModel->GetUser();
        $numUsers = $userModel->GetNumUser();
        
        $this->view->users = $users;
        $this->view->numUsers = $numUsers;
        $this->view->resultAdd = "";
        if ($this->_request->isPost()) {
        	$id = $userModel->GetMaxIDUser();
        	//$id = $id + 1;
        	$username = $_POST['txt_Username']; 
       		$password = md5($_POST['txt_Password']);
       		$decentralize = $_POST['list_Decentralize'];
       		$email = $_POST['txt_Email'];
       		$fullname = $_POST['txt_Fullname'];
       		$this->view->resultAdd = $userModel->AddUser($id, $username, $password, $decentralize, $email, $fullname);
       		$this->_redirect('./user/manager');
        }
    }

    public function editAction(){
    	/*if ($this->_request->isPost()){
    		$id = $_POST['txtIdEdit'];
    		include_once dirname(__FILE__).'./../models/UserModel.php';
       		$userModel = new UserModel();
       		$temp = $userModel->GetInfoFromID($id);
       		
       		$this->view->temp = $temp;
    	}else {
    		$this->view->temp = "No Post";
    	}*/
    	$id = $this->_request->getParam('id');
    	include_once dirname(__FILE__).'./../models/UserModel.php';
       	$userModel = new UserModel();
       	$temp = $userModel->GetInfoFromID($id);
       	
       	$this->view->temp = $temp;
    }
    
    public function saveAction(){
    	if ($this->_request->isPost()) {
    		include_once dirname(__FILE__).'./../models/UserModel.php';
       		$userModel = new UserModel();
       		$id = $_POST['txt_ID'];
       		$username = $_POST['txt_Username']; 
       		$password = md5($_POST['txt_Password']);
       		$decentralize = $_POST['list_Decentralize'];
       		$email = $_POST['txt_Email'];
       		$fullname = $_POST['txt_Fullname'];
       		$userModel->UpdateUser( $id, $username, $password, $decentralize, $email, $fullname);
       		
       		$this->view->temp = "Save Successfull";
    	}else {
    		$this->view->temp = "No Post";
    	}
    }
    
    public function deleteAction(){
    	include_once dirname(__FILE__).'./../models/UserModel.php';
       	$userModel = new UserModel();
       	$id = $this->_request->getParam('id');
       	$this->view->temp = $userModel->DeleteUser($id);
    }
    
}



